/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MathLib.triangulation;

import OptionsPack.OptionsStorage;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JTextArea;

/**
 *
 * @author mark_o
 */
public class Triangulation implements Runnable{
    //protected IFigure fig;
    //public static String TriangulatorAdress = "\"triangle.exe\"";
    public static String TriangulatorAdress = "\"C:\\Documents and Settings\\mark\\My Documents\\NetBeansProjects\\MySimpleFEM\\src\\triangle.exe\"";

    public OptionsStorage opt;
    private String path;
    protected JTextArea text;

    public Triangulation() {
    
    }

    public Triangulation(OptionsStorage opt, String path,JTextArea text) {
        this.opt = opt;
        this.path = path;
        this.text = text;
    }
    
    public void triangulate(){
        
        try
        {

            StringBuilder command = new  StringBuilder();
            if(!opt.TriangleAngle.equals("-1")){
                command.append("-q"+opt.TriangleAngle+" ");
            }else{
                command.append("-q ");
            }
            
            command.append("-D ");

            command.append("-a"+opt.TriangleArea+" ");
            System.out.println("-a"+opt.TriangleArea+" ");
            command.append(opt.TRIANGULATION_ALGORITH+" ");

            Process proc =  Runtime.getRuntime().exec(TriangulatorAdress+command+"\""+path+"\"");


            
            InputStream stderr = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            text.setText("");
            text.setFont(new Font("Calibri", Font.ITALIC, 14));
            text.append("Для Триангуляції використано OpenSource генератор.\n" +
                    "Triangle\nA Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\nVersion 1.6" +
                    "Copyright 1993, 1995, 1997, 1998, 2002, 2005 Jonathan Richard Shewchuk\n2360 Woolsey #H\n" +
                    "Berkeley, California  94705-1927\nPlease send bugs and comments to jrs@cs.berkeley.edu");
            while ( (line = br.readLine()) != null)
                text.append(line+"\n");
            text.append("Триангуляція завершена");
            int exitVal = proc.waitFor();
            text.append("Process exitValue: " + exitVal);
        } catch (Throwable t)
          {
            t.printStackTrace();
          }

    }

    public void run() {
        triangulate();
    }

    
    
}
